﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using RLAWEB.Models;
using RLAWEB.RLAServiceReference;

namespace RLAWEB
{
    public partial class CreateAchievement : Page
    {
        private RealLifeServiceClient _client;
        protected void Page_Load(object sender, EventArgs e)
        {
            _client = new RealLifeServiceClient();
            
            if (!IsPostBack)
            {
                PopulateCategoryDropedown();
                listFriend.DataSource = _client.GetFriendships(GetLoggedInUser());
                listFriend.DataBind();

                addedFriends.DataSource = RetriveFriendsFromURL();
                addedFriends.DataBind();
            }

            

            InsertData();
        }

        private List<User> RetriveFriendsFromURL()
        {
            List<User> result = new List<User>();

            string urlFriends = Request.QueryString["AccF"];

            if (urlFriends != null)
            {
                string[] friends = urlFriends.Split('&');

                foreach (string id in friends)
                {
                    result.Add(_client.GetUserById(Int32.Parse(id)));
                }
            }
            return result;
        }

        private User GetLoggedInUser()
        {
            User loggedUser = Helper.GetLoggedInUser(Request.Cookies[FormsAuthentication.FormsCookieName]);

            if (loggedUser != null)
            {
                return loggedUser;
            }
            else
            {
                //TODO: redirect to login page
                return null;
            }
        }

        protected void CreateMyAchievement(RLAServiceReference.Achievement ach)
        {
            if (_client == null)
                _client = new RealLifeServiceClient();
            var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                User loggedInUser = Helper.GetLoggedInUser(authCookie);
                
                _client.CreateAchievement(loggedInUser, ach);
            }
        }

        protected void PopulateCategoryDropedown()
        {
            List<String> categories = new List<string>() {"Kategori 1", "Kategori 2", "Kategori 3"};

            ddlCategories.DataSource = categories;
            ddlCategories.DataBind();
        }

        protected void btnCreateAchievement_Click(object sender, EventArgs e)
        {

            RLAServiceReference.Achievement ach = new RLAServiceReference.Achievement()
                                                  {
                                                      Category = ddlCategories.SelectedItem.Text,
                                                      Criteria = "Not implemented.",
                                                      CurrentStatus = RLAServiceReference.Achievement.Status.Active,
                                                      Title = txtTitle.Text,
                                                      Description = txtDescription.Text,
                                                      Tag = txtTags.Text,
                                                      Participants = GetParticipants()

                                                  };

            CreateMyAchievement(ach);
            Response.Redirect("ListAchievements.aspx");
        }

        protected List<User> GetParticipants()
        {
            return new List<User>();
        }

        protected void ClearFields()
        {
            ddlCategories.ClearSelection();
            txtTitle.Text = "";
            txtDescription.Text = "";
            txtTags.Text = "";
        }

        protected void InsertData()
        {
            var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                User loggedInUser = Helper.GetLoggedInUser(authCookie);

                List<User> friends = _client.GetFriendships(loggedInUser);
               
            }
        }

        protected void btnAddFriend_Click(object sender, EventArgs e)
        {
            Button btn = (Button) sender;
            btn.Visible = false;

            string strFriendID = Request.QueryString["ID"];

            string acceptedFriends = Request.QueryString["AccF"];
            if (acceptedFriends != null)
            {
                string newFriend = Request.QueryString["ID"];
                acceptedFriends += "&" + newFriend;
            }
            else
            {
                acceptedFriends = strFriendID;

                string url = HttpContext.Current.Request.Url.PathAndQuery;

                //TODO: check if ID is set (then it needs to be ?AccF= ..

                Response.Redirect(url + "&AccF=" + strFriendID);
            }
        }
    }
}